from manim import *
class math(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{设函数}$f\left( x\right) $\text{在}$\left( -\infty ,+\infty \right) $\text{上满足}$f\left( x\right) =f\left( x-\pi \right) +\sin x,$"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        text1 = Tex(r"\text{且}$f\left( x\right)=x,x\in [  0,\pi ) , $\text{求}$f\left( x\right)$\text{在}$[  \pi ,3\pi ) $\text{上的表达式}"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text),Write(text1),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,25,0]),text1.animate().move_to([0,20,0])  )
        rectangle=Rectangle(color=BLUE,fill_opacity=0.3,width=22,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-15, 20, 0])
        self.play(Create(rectangle))
        text2 = Tex(r"\text{当}$0\leq x <\pi  $\text{时,}$f\left( x\right)=x$"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,10,0])
        self.play(ReplacementTransform(rectangle,text2))
        self.wait(0.2)
        arrow = Arrow(start=[-30,7,0],end=[-30,2,0],color=RED
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow))
        text3 = Tex(r"\text{当}$0\leq \Delta  <\pi  $\text{时,}$f\left( \Delta \right)=\Delta $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-1,0])
        self.play(Write(text3))
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.3,width=32,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([13.5, 20, 0])
        
        rectangle10=Rectangle(color=BLUE,fill_opacity=0.3,width=32,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle10.move_to([13.5, 20, 0])
        self.play(Create(rectangle10),Create(rectangle1))
        rectangle2=Rectangle(color=YELLOW,fill_opacity=0.3,width=30,height=5
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle2.move_to([-30,-1,0])
      
        rectangle20=Rectangle(color=YELLOW,fill_opacity=0.3,width=30,height=5
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle20.move_to([-30,-1,0])
        self.play(Create(rectangle2),Create(rectangle20))

        text4 = Tex(r"\text{当}$\pi \leq x <2\pi $\text{时}",color=RED
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,10,0])
        self.play(ReplacementTransform(rectangle1,text4))
        self.wait(0.2)
        text5 = Tex(r"\text{有}$0\leq x-\pi  <\pi  $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        self.play(Write(text5))
        text6 = Tex(r"$f\left( x-\pi \right) =x-\pi  $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(ReplacementTransform(rectangle2,text6))
        self.wait(0.2)
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.3,width=32,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle3.move_to([17.5, 25, 0])
        self.play(Create(rectangle3))
        text7 = Tex(r"$f\left( x\right) =\left( x-\pi \right) +\sin x $",color=YELLOW
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(ReplacementTransform(rectangle3,text7))


        text40 = Tex(r"\text{当}$2\pi \leq x <3\pi $\text{时}",color=RED
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,10,0])
        self.play(ReplacementTransform(rectangle10,text40))
        self.wait(0.2)
        text50 = Tex(r"\text{有}$0\leq x-2\pi  <\pi  $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,5,0])
        self.play(Write(text50))
        text60 = Tex(r"$f\left( x-2\pi \right) =x-2\pi  $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,0,0])
        self.play(ReplacementTransform(rectangle20,text60))
        self.wait(0.2)
        rectangle30=Rectangle(color=BLUE,fill_opacity=0.3,width=32,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle30.move_to([17.5, 25, 0])
        
        rectangle300=Rectangle(color=BLUE,fill_opacity=0.3,width=32,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle300.move_to([17.5, 25, 0])
        self.play(Create(rectangle300),Create(rectangle30))
        text60 = Tex(r"$f\left( x\right) =f\left( x-\pi \right) +\sin x$"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-15,0])
        text61 = Tex(r"$f\left( x-\pi \right) =f\left( x-2\pi \right) +\sin \left( x-\pi \right)  $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-20,0])
        self.play(ReplacementTransform(rectangle30,text60),ReplacementTransform(rectangle300,text61))
        self.wait(0.2)
        

        text62 = Tex(r"$f\left( x\right) =f\left( x-2\pi \right)+\sin \left( x-\pi \right) +\sin x $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-15,0])
        self.play(ReplacementTransform(text61,text62),ReplacementTransform(text60,text62))
        self.wait(0.2)
        
        text63 = Tex(r"$f\left( x\right) =x-2\pi $",color=YELLOW
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-5,0])
        self.play(ReplacementTransform(text62,text63))
        self.wait(0.2)
        rectangle5=Rectangle(color=RED,fill_opacity=0.0,width=56,height=20
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle5.move_to([15, 2.5, 0])
        self.play(Create(rectangle5))
        text66 = Tex(r"$f\left( x\right) =\left\{\begin{matrix}x-\pi +\sin x &, \pi \leq x <2\pi \\x-2\pi  &,2\pi \leq x <3\pi \end{matrix}\right.$",color=GREEN
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-17.5,0])
        self.play(ReplacementTransform(rectangle5,text66))
        self.wait(0.2)

        self.wait()